import { ElMessage } from 'element-plus'
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'



const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'home',
    component: () => import('../views/Home.vue'),
    children:[
      {
        path:'home',
        name:'home1',
        meta:{
          isShow:true,
          title:'首页'
        },
        component: () => import('../views/System.vue')
      },
      {
        path:'goods',
        name:'goods',
        meta:{
          isShow:true,
          title:'商品管理'
        },
        component: () => import('../views/GoodsView.vue')
      },
      {
        path:'user',
        name:'user',
        meta:{
          isAdmin:true,
          title:'用户管理'
        },
        component: () => import('../views/USerView.vue')
      },
      {
        path:'order',
        name:'order',
        meta:{
          isShow:true,
          title:'订单管理'
        },
       
        component: () => import('../views/OrderView.vue'),
      },
      {
        path:'log',
        name:'log',
        meta:{
          isAdmin:true,
          title:'登录日志'
        },
        component: () => import('../views/LogView.vue')
      },
      {
        path:'dataAnalysis',
        name:'dataAnalysis',
        meta:{
          isShow:true,
          title:'订单数据分析'
        },
        component: () => import('../views/OrderDataAnalysis.vue')
      },
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Loginvue.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/Register.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
router.beforeEach((to, from, next) => {
  const isLogin = localStorage.getItem('adminToken')
  // console.log(to.path);
  
  if(['/register', '/login'].includes(to.path)) {
    next()
  }
  if(!isLogin && to.path != '/login') {
    ElMessage.error('请先登录')
    next('/login')
    return
  }
  else {
    console.log(to.fullPath ,from);
    localStorage.setItem('url', to.fullPath)
    next()
  }
})
export default router
